#include <iostream>
#include <algorithm>
using namespace std;

typedef struct Birth
{
	int year;
	int month;
	int day;
	int id;
	string name;
}Bi;

bool cmp(Bi s1, Bi s2)
{
	if(s1.year != s2.year)
		return s1.year < s2.year;
	else if(s1.month != s2.month)
		return s1.month < s2.month;
	else if(s1.day != s2.day)
		return s1.day < s2.day;
	else
		return s1.id > s2.id;
}

int main()
{
	Bi s[110];
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> s[i].name >> s[i].year >> s[i].month >> s[i].day;
		s[i].id = i;
	}
	sort(s, s + n, cmp);
	for(int i = 0; i < n; i++)
	{
		cout << s[i].name << endl;
	}
	return 0;
}
